import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chat/common/style/styles.dart';

class ToastUtil {
  /// 展示时长
  static const Duration _duration = Duration(seconds: 2);

  /// 动画延迟
  static const Duration _animationDuration = Duration(milliseconds: 350);

  /// 关闭所有Toast加载
  static void closeAllLoading() {
    BotToast.closeAllLoading();
  }

  /// 文字Toast
  static void text(String text) {
    BotToast.showText(
      text: text,
      textStyle: const TextStyle(
        fontSize: AppTheme.smallFont,
        color: Colors.white,
      ),
    );
  }

  /// 成功的通知Toast
  static void successNotification({
    String title = "提示",
    String? subTitle,
    bool enableClose = true,
    Function()? onTap,
    Function()? onClose,
    Duration? duration,
    bool onlyOne = false,
    bool enableSlideOff = true,
  }) {
    notification(
      title: title,
      subTitle: subTitle,
      enableClose: enableClose,
      onTap: onTap,
      onClose: onClose,
      duration: duration,
      icon: Icons.check_circle_outline,
      iconColor: AppTheme.successColor,
      onlyOne: onlyOne,
      enableSlideOff: enableSlideOff,
    );
  }

  /// 错误的通知Toast
  static void errorNotification({
    String title = "错误",
    String? subTitle,
    bool enableClose = true,
    Function()? onTap,
    Function()? onClose,
    Duration? duration,
    bool onlyOne = false,
    bool enableSlideOff = true,
  }) {
    notification(
      title: title,
      subTitle: subTitle,
      enableClose: enableClose,
      onTap: onTap,
      onClose: onClose,
      duration: duration,
      icon: Icons.highlight_off,
      iconColor: AppTheme.dangerColor,
      onlyOne: onlyOne,
      enableSlideOff: enableSlideOff,
    );
  }

  /// 警告的通知Toast
  static void warningNotification({
    String title = "警告",
    String? subTitle,
    bool enableClose = true,
    Function()? onTap,
    Function()? onClose,
    Duration? duration,
    bool onlyOne = false,
    bool enableSlideOff = true,
  }) {
    notification(
      title: title,
      subTitle: subTitle,
      enableClose: enableClose,
      onTap: onTap,
      onClose: onClose,
      duration: duration,
      icon: Icons.error_outline,
      iconColor: AppTheme.warningColor,
      onlyOne: onlyOne,
      enableSlideOff: enableSlideOff,
    );
  }

  /// 通知Toast
  static void notification({
    required String title,
    String? subTitle,
    bool? enableClose,
    Function()? onTap,
    Function()? onClose,
    Duration? duration,
    IconData? icon,
    Color? iconColor,
    bool onlyOne = false,
    bool enableSlideOff = true,
  }) {
    BotToast.showNotification(
      leading: (cancel) => SizedBox.fromSize(
        size: const Size(40, 40),
        child: Icon(
          icon ?? Icons.notifications,
          color: iconColor ?? AppTheme.primaryColor,
          size: 30,
        ),
      ),
      trailing: enableClose == true
          ? (cancel) => IconButton(
                icon: const Icon(
                  Icons.cancel,
                  color: AppTheme.dangerColor,
                ),
                onPressed: cancel,
              )
          : null,
      // 是否只显示一个
      onlyOne: onlyOne,
      // 是否能滑动删除
      enableSlideOff: enableSlideOff,
      // 构建标题
      title: (cancelFunc) {
        return Text(title);
      },
      // 构建副标题
      subtitle: subTitle != null
          ? (cancelFunc) {
              return Text(subTitle);
            }
          : null,
      // 展示时长
      duration: duration ?? _duration,
      // 动画延迟
      animationDuration: _animationDuration,
      // 动画反向延迟
      animationReverseDuration: _animationDuration,
      // 点击事件
      onTap: onTap,
      // 关闭事件
      onClose: onClose,
    );
  }
}
